package config

import (
    "asset-management-go/global"
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
    "log"
    "time"
)

func InitMysql() {
    dsn := Appconfig.Database.Mysql.User + ":" + Appconfig.Database.Mysql.Password + "@tcp(" + Appconfig.Database.Mysql.Host + ":" + Appconfig.Database.Mysql.Port + ")/" + Appconfig.Database.Mysql.Name + "?charset=utf8mb4&parseTime=True&loc=Local"
    db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})

    if err != nil {
        log.Fatalf("Mysql连接异常：%v", err)
    }

    sqlDB, err := db.DB()

    sqlDB.SetMaxIdleConns(Appconfig.Database.Mysql.MaxIdle)
    sqlDB.SetMaxOpenConns(Appconfig.Database.Mysql.MaxActive)
    sqlDB.SetConnMaxLifetime(time.Hour)

    global.Mysql = db
}
